/**
 * 
 * @author 梁国玺
* @Date 2023-09-15 20:49:52
*/

import _ from 'lodash'
import { Message } from 'element-ui'
/**
 * 过滤数据
 * @author 梁国玺
* @Date 2023-09-15 21:02:33
*/
export function getResult(res) {
    const code = _.get(res, 'code')
    const success = _.get(res, 'success')
    if (success && code === 0) {
        return _.get(res, 'data')
    } else {
        const message = _.get(res, 'msg')
        if (message) {
            Message({ message: message || 'Error', type: 'error', duration: 4 * 1000 })
        }
        // const error = new Error(message)
        // throw enhanceError(error, res.config, _.get(res, 'data.StatusCode'), res.request, res.data)
    }
}

/**
 * Update an Error with the specified config, error code, and response.
 *
 * @param {Error} error The error to update.
 * @param {Object} config The config.
 * @param {string} [code] The error code (for example, 'ECONNABORTED').
 * @param {Object} [request] The request.
 * @param {Object} [response] The response.
 * @returns {Error} The error.
 */
export function enhanceError(error, config, code, request, response) {
    error.config = config
    if (code) {
        error.code = code
    }

    // error.request = request
    // error.response = response
    error.isAxiosError = true

    error.toJSON = () => {
        return {
            // Standard
            message: this.message,
            name: this.name,
            // Microsoft
            description: this.description,
            number: this.number,
            request: request,
            data: response,
            // Mozilla
            // fileName: this.fileName,
            // lineNumber: this.lineNumber,
            // columnNumber: this.columnNumber,
            // stack: this.stack,
            // Axios
            config: this.config,
            code: this.code
        }
    }
    return error
}

/**
 * 拼接url 方法
 * 删除value 为空的 参数
 * @param {*} url
 * @param {*} params
 */
export function query(url, params) {
    if (!params) return url
    const p = _.omitBy(params, v => !v && v !== 0)
    let queryString = ''
    for (const key in p) {
        if (p[key] instanceof Array) {
            queryString = queryString ? `${queryString}&${key}=${p[key].join(',')}` : `${key}=${p[key].join(',')}`
        } else {
            queryString = queryString ? `${queryString}&${key}=${p[key]}` : `${key}=${p[key]}`
        }
    }
    return queryString ? `${url}?${queryString}` : url
}

